using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TankWars
{
    public class TankTurret : MovableSprite
    {
        private Tank tank;
        private Vector2 vOffset = Vector2.Zero;

        /// <summary>
        /// Gets the origin of the turret that it should rotate on
        /// </summary>
        private new Vector2 Origin
        {
            get
            {
                return new Vector2(tank.tankWidth / 2, tank.tankHeight / 2);
            }
        }

        /// <summary>
        /// Gets the position vector at the end of the tank turret
        /// </summary>
        public Vector2 EndOfTurret
        {
            get
            {
                Vector2 vEndOfTurret = base.vPosition;

                //Get the amount of X and Y that needs to be added to subtracted
                //to the position to get the end of the turret
                float fXAdd = base.Sprite.Width * (float)Math.Cos(base.fRotation);
                float fYAdd = base.Sprite.Width * (float)Math.Sin(base.fRotation);

                //First quadrant
                if (MathHelper.ToDegrees(base.fRotation) < 90)
                {
                    vEndOfTurret.X += fXAdd;
                    vEndOfTurret.Y += fYAdd;
                }
                //Second quadrant
                else if (MathHelper.ToDegrees(base.fRotation) < 180)
                {
                    vEndOfTurret.X -= fXAdd;
                    vEndOfTurret.Y += fYAdd;
                }
                //Third quadrant
                else if (MathHelper.ToDegrees(base.fRotation) < 270)
                {
                    vEndOfTurret.X -= fXAdd;
                    vEndOfTurret.Y -= fYAdd;
                }
                //Fourth quadrant
                else
                {
                    vEndOfTurret.X += fXAdd;
                    vEndOfTurret.Y -= fYAdd;
                }

                return vEndOfTurret;
            }
        }

        public TankTurret(Texture2D turretTexture, Tank owningTank)
        {
            base.Sprite = turretTexture;
            this.tank = owningTank;
        }

        public TankTurret(Texture2D turretTexture, Tank owningTank, Vector2 vTurretOffset)
        {
            base.Sprite = turretTexture;
            this.tank = owningTank;
            this.vOffset = vTurretOffset;
        }

        /// <summary>
        /// Draws the tank turret at the origin of the tank, pointing towards the mouse
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="mouseState"></param>
        public new void Draw(SpriteBatch spriteBatch, Map map)
        {
            spriteBatch.Draw(base.Sprite, map.GetTrueScreenPosition(this.tank.vPosition), null, this.tank.TankColor, base.fRotation, this.Origin, 1.0f, SpriteEffects.None, 0.0f); 
        }

        /// <summary>
        /// Updates the rotation of the turret
        /// </summary>
        /// <param name="mouseState"></param>
        public void UpdatePosition(MouseState mouseState, Map map)
        {
            base.fRotation = this.GetMouseRotation(mouseState, map);
        }

        /// <summary>
        /// Returns the angle the mouse is at compared to the tank
        /// </summary>
        /// <param name="mouseState"></param>
        /// <returns></returns>
        private float GetMouseRotation(MouseState mouseState, Map map)
        {
            //Get the mouse coordinates
            float fMouseX = mouseState.X;
            float fMouseY = mouseState.Y;

            float fTankX = map.GetTrueScreenPosition(this.tank.vPosition).X;
            float fTankY = map.GetTrueScreenPosition(this.tank.vPosition).Y;

            //Use arc tangent to get the angle.
            float fRoatation = (float)Math.Atan2(fTankY - fMouseY, fMouseX - fTankX);

            //Ensure that the angle is in the proper quadrant based
            //on the given coordinates of the mouse and the tank

            //First Quadrant
            if ((fMouseX >= fTankX) && (fMouseY <= fTankY))
            {
                while ((MathHelper.ToDegrees(fRoatation) > 90) || (MathHelper.ToDegrees(fRoatation) < 0))
                {
                    fRoatation += MathHelper.ToRadians(90);
                    if (fRoatation > MathHelper.ToRadians(360))
                    {
                        fRoatation -= MathHelper.ToRadians(360);
                    }
                }
            }
            //Second Quadrant
            else if ((fMouseX <= fTankX) && (fMouseY <= fTankY))
            {
                while ((fRoatation > MathHelper.ToRadians(180)) || (fRoatation < MathHelper.ToRadians(90)))
                {
                    fRoatation += MathHelper.ToRadians(90);
                    if (fRoatation > MathHelper.ToRadians(360))
                    {
                        fRoatation -= MathHelper.ToRadians(360);
                    }
                }
            }
            //Third Quadrant
            else if ((fMouseX <= fTankX) && (fMouseY >= fTankY))
            {
                while ((fRoatation > MathHelper.ToRadians(270)) || (fRoatation < MathHelper.ToRadians(180)))
                {
                    fRoatation += MathHelper.ToRadians(90);
                    if (fRoatation > MathHelper.ToRadians(360))
                    {
                        fRoatation -= MathHelper.ToRadians(360);
                    }
                }
            }
            //Fourth Quadrant
            else
            {
                while ((fRoatation > MathHelper.ToRadians(360)) || (fRoatation < MathHelper.ToRadians(270)))
                {
                    fRoatation += MathHelper.ToRadians(90);
                    if (fRoatation > MathHelper.ToRadians(360))
                        fRoatation -= MathHelper.ToRadians(360);
                }
            }

            fRoatation = MathHelper.ToRadians(360) - fRoatation;
            return fRoatation;
        }
    }
}
